import com.facebook.commands.photos.UploadPhoto;
import com.facebook.data.photos.AlbumData;

import flash.events.MouseEvent;

import mx.events.ListEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import source_files.collapse;

[Embed (source="images/drag_icon.png")]
[Bindable]public var dragTargetImage:Class;

protected var ad:AlbumData;
protected var fileStream:FileStream;
protected var defaultAlbum:Boolean;
protected var uploadPhoto:UploadPhoto

private function init():void {
	initObjects();
	initFacebookSession();
	initPlayer();
	initEvents();
	initTwitter();
}

private function initObjects():void {
	topPanel.navBar.dataProvider = middlePanelWrapper.middleStack;
	bottomPanelWrapper.albumsDataGrid.dataProvider = facebookPhotoAlbums;
	bottomPanelWrapper.photosList.dataProvider = facebookPhotos;
	
	/** 
	 * 	youtube remote object
	 **/ 
	youtubeRemoteObject.youtubeRemote.addEventListener(FaultEvent.FAULT, faultListener);
	youtubeRemoteObject.youtubeRemote.getOperation('searchVideo').addEventListener(ResultEvent.RESULT, searchListener);
	
	// top links data linking
	topPanel.navBar.dataProvider = middlePanelWrapper.middleStack;
	
	//middlePanelWrapper.dragTarget.source = dragTargetImage;
	fileStream = new FileStream();
}

private function initEvents():void {
	var collapseLeftPanel:Function = function():void {
		collapse.collapsePanel(leftPanelWrapper);
	}
	
	var facebookLogin:Function = function():void {
		session.login();
	}
	
	var searchYouTuBe:Function = function():void {
		doSearch(middlePanelWrapper.artist.text);
	}
	
	var getTweetsfromTwitter:Function = function():void {
		getTweets();
	}
	
	var playYouTuBeVideo:Function = function(event:ListEvent):void {
		playVideo(event);
	}
	
	
	var postingTweet:Function = function():void {
		postTweet(middlePanelWrapper.tweet.text);
	}
	
	var getFollowersfromTwitter:Function = function():void {
		getFollowers();
	}
	
	var doneLogFacebook:Function = function():void {
		doneLoggingIn();
	}
	
	var getAlbums:Function = function():void {
		getPhotoAlbums();
	}
	
	var changeSelectedAlbum:Function = function():void {
		showSelectedAlbum();
	}

	var showPhotos:Function = function():void {
		middlePanelWrapper.fullsizePhoto.source = bottomPanelWrapper.photosList.selectedItem.src_big;		
	}
	
	leftCollapse.btn.addEventListener(MouseEvent.CLICK, collapseLeftPanel);
	leftPanelWrapper.loginFacebook.addEventListener(MouseEvent.CLICK, facebookLogin);
	
	middlePanelWrapper.btnSearch.addEventListener(MouseEvent.CLICK, searchYouTuBe);
	middlePanelWrapper.btngetTweets.addEventListener(MouseEvent.CLICK, getTweetsfromTwitter);
	middlePanelWrapper.searchResults.addEventListener(ListEvent.CHANGE, playYouTuBeVideo);
	middlePanelWrapper.goTweet.addEventListener(MouseEvent.CLICK, postingTweet);
	middlePanelWrapper.btnlog.addEventListener(MouseEvent.CLICK, doneLogFacebook);
	middlePanelWrapper.btnphoto.addEventListener(MouseEvent.CLICK, getAlbums);
	middlePanelWrapper.syncPost.addEventListener(MouseEvent.CLICK, doSimultaneousPost);
	
	bottomPanelWrapper.albumsDataGrid.addEventListener(ListEvent.CHANGE, changeSelectedAlbum);
	bottomPanelWrapper.photosList.addEventListener(MouseEvent.CLICK, showPhotos);
	
	addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragEnter, false, 0, true);
	addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop, false, 0, true);	
}

/**
 * Handler when user drags and image over the target.
 * Will determine to accept drop or not.
 */ 
protected function onDragEnter(event:NativeDragEvent):void {
	if (event.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) {
		NativeDragManager.acceptDragDrop(this);
	}
}

/**
 * After user drop image, we check against Facebook's supported image types.
 * http://wiki.developers.facebook.com/index.php/Photos.upload
 * Once confirmed type is valid we add image to the stage.
 */ 
protected function onDragDrop(event:NativeDragEvent):void {
	var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
	
	for each( var file:File in files) {
		var ext:String = file.extension.toLowerCase();
		switch(ext) {
			case 'png':
			case 'jpg':
			case 'jpeg':
			case 'gif':
			case 'xbm':
			case 'wbmp':
			case 'iff':
			case 'jp2':
			case 'tiff':
			case 'psd':
				uploadImage(file);
				break;
			default:
				Alert.show("Error -- File is not a supported format");
		}
	}
}

/**
 * Receives file object, fileStream will open/read file's information creating a byte array 
 * that is used to upload to Facebook.
 * Checking for a "default" album and a selected album
 */ 
protected function uploadImage(fileInfo:File):void {
	fileStream.open(fileInfo, FileMode.READ);
    var imageData:ByteArray = new ByteArray();
    fileStream.readBytes(imageData);
    ad = bottomPanelWrapper.albumsDataGrid.selectedItem as AlbumData;

    if (bottomPanelWrapper.albumsDataGrid.selectedIndex == -1) {
		defaultAlbum = true;
	} else {
		defaultAlbum = false;
	}  
    if (!defaultAlbum) {
    	Alert.show("You are uploading to album: " + ad.name,'Photo upload' ,Alert.OK);
    	uploadPhoto = fbook.post(new UploadPhoto(imageData, ad.aid, fileInfo.name+ ' : ' + new Date().getTime(),null)) as UploadPhoto;
    } else {
    	Alert.show("You are uploading to the applications default album.");
    	uploadPhoto = fbook.post(new UploadPhoto(imageData)) as UploadPhoto;
    }
}

private function doSimultaneousPost(event:MouseEvent):void
{
	var text:String = middlePanelWrapper.syncMessage.text;
	if (middlePanelWrapper.syncFacebook.selected) {
		postOnWall(text);	
	}
	if (middlePanelWrapper.syncTwitter.selected) {
		postTweet(text);
	}
	
}